package com.suyx.job.job;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeanUtils;

import com.suyx.job.entity.DeviceInfo;
import com.suyx.job.entity.JobInfo;
import com.suyx.job.entity.JobLog;
import com.suyx.job.utils.CommandUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: lanyanhua
 * @Date: 2018/10/17 0017 14:45
 * @Description:
 */
@Slf4j
public class SynDeviceStatusJob extends ExecuteJob {

	@Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("进入了---SynDeviceStatusJob : ");
        JobDataMap param = jobExecutionContext.getMergedJobDataMap();
        jobInfo = (JobInfo) param.get("jobInfo");
        String subject = "项目：" + jobInfo.getJobGroup() + ",执行任务：" + jobInfo.getJobName();
        log.info("id:" + jobInfo.getJobId() + "," + subject);
        
        List<DeviceInfo> deviceInfos = deviceService.findAll();
        for(DeviceInfo device :deviceInfos){
        	DeviceInfo deviceUpdate = new DeviceInfo();
        	BeanUtils.copyProperties(device,deviceUpdate);
        	String result  = CommandUtils.excuteCommand("adb -s "+ device.getDeviceCode()  +" get-state");
        	if(StringUtils.isNotEmpty(result) && "device".equals(result.trim())){
        		deviceUpdate.setStatus(1);
        	}else{
        		deviceUpdate.setStatus(0);
        	}
        	deviceService.saveAndFlush(deviceUpdate);
        }
        log.info("SynDeviceStatusJob执行完成~");
    }

    

}
